import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

const req = require.context('../views/', true, /\.vue$/)

const routeArr = req
  .keys()
  .map((item) => req(item).default.__file.split('/views/')[1].split('/')[0])

const newRouterArr = routeArr.filter(
  (item, index) => routeArr.findIndex((value) => value === item) === index
)
console.log(newRouterArr)
const err = []
newRouterArr.forEach((item) => {
  err.push({
    path: '/',
    component: Layout,
    redirect: '/' + item,
    children: [
      {
        path: '',
        name: item,
        component: () => import(`@/views/${item}/index`),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  })
})
err.forEach((item) => {
  console.log(item.children[0].component)
})

export const constantRoutes=err


const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
